package com.area.service.impl;

import com.area.entity.Brand;
import com.area.mapper.BrandMapper;
import com.area.service.IBrandService;
import com.code.util.Variable;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author guanfenga@yonyou.com
 * @since 2023-01-10
 */
@Service
@Slf4j
@Transactional
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements IBrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Brand> queryBrandAll(Brand brand) {
        // 用于清除缓存 原因是手动修改数据库图片所以需要进行该操作
        //redisTemplate.delete("brandList");
        //查询缓存中是否已经存在了品牌列表
        List<Brand> brandList = (List<Brand>) redisTemplate.opsForValue().get(Variable.BRAND_LIST);

        if( brandList == null){
            brandList = brandMapper.queryBrandAll(brand);
        }
        log.info(String.format("写入redis数据======>%d,条数",brandList.size()));
        return brandList;
    }

    @Override
    public Brand queryBrandId(Integer brandId) {
        return brandMapper.queryBrandId(brandId);
    }
}
